package utils

import (
	"crypto/md5"
	"encoding/hex"
	"io"
	"os"
)

func Md5File(file io.Reader) (sum string, err error){
	//TODO 优化方向: 用缓冲区进行流式计算
	md5obj := md5.New()

	_,err = io.Copy(md5obj,file)
	if(err != nil){
		return "",err
	}

	sum = hex.EncodeToString(md5obj.Sum(nil))

	return sum,nil

}


func PathExists(path string) (bool, error) {
	_, err := os.Stat(path)
	if err == nil {
	return true, nil
	}
	if os.IsNotExist(err) {
	// 创建文件夹
		err := os.MkdirAll(path, os.ModePerm)
		if err != nil {
			return false,err
		} else {
			return true, nil
		}
	}
	return false, err
}


